using RockHopper.Common.Data;
using RockHopper.Common.Events;
using RockHopper.Common.Services;
using RockHopper.Common.ViewModel;

namespace RockHopper.TrainingCalculator.WinRT.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        private readonly ICalculationService calculationService;
        private readonly ISettingsRepository settingsRepository;

        public MainViewModel(ICalculationService calculationService, ISettingsRepository settingsRepository)
        {
            this.calculationService = calculationService;
            this.settingsRepository = settingsRepository;

            VdotTile = RegisterViewModel<VdotTileViewModel>();
            EasyTile = RegisterViewModel<EasyTileViewModel>();
            ThresholdTile = RegisterViewModel<ThresholdTileViewModel>();
            IntervalTile = RegisterViewModel<IntervalTileViewModel>();
            RepetitionTile = RegisterViewModel<RepetitionTileViewModel>();
            MarathonTile = RegisterViewModel<MarathonTileViewModel>();
            RacePredictorTile = RegisterViewModel<RacePredictorTileViewModel>();
            WindEffectTile = RegisterViewModel<WindEffectTileViewModel>();
            TempEffectTile = RegisterViewModel<TempEffectTileViewModel>();
            HeartRateTile = RegisterViewModel<HeartRateTileViewModel>();
            Units = RegisterUnitsViewModel();
            General = (GeneralViewModel)Global.Container.Resolve<ViewModelBase>(typeof(GeneralViewModel).Name);

            DomainEvents.VdotChanged += (s, e) => UpdateTilesFromContext();
            DomainEvents.PaceUnitsChanged += (s, e) => UpdateTilesFromContext();

            UpdateTilesFromContext();
        }

        public VdotTileViewModel VdotTile { get; private set; }
        public EasyTileViewModel EasyTile { get; private set; }
        public ThresholdTileViewModel ThresholdTile { get; private set; }
        public IntervalTileViewModel IntervalTile { get; private set; }
        public RepetitionTileViewModel RepetitionTile { get; private set; }
        public MarathonTileViewModel MarathonTile { get; private set; }
        public RacePredictorTileViewModel RacePredictorTile { get; private set; }
        public WindEffectTileViewModel WindEffectTile { get; private set; }
        public TempEffectTileViewModel TempEffectTile { get; private set; }
        public HeartRateTileViewModel HeartRateTile { get; private set; }
        public UnitsViewModel Units { get; private set; }
        public GeneralViewModel General { get; private set; }

        private void UpdateTilesFromContext()
        {
            var context = calculationService.Calculate(settingsRepository.Vdot);
            var units = settingsRepository.PaceUnits;
            VdotTile.UpdateLiveTile(context, units);
            EasyTile.UpdateLiveTile(context, units);
            MarathonTile.UpdateLiveTile(context, units);
            ThresholdTile.UpdateLiveTile(context, units);
            IntervalTile.UpdateLiveTile(context, units);
            RepetitionTile.UpdateLiveTile(context, units);
        }

        private T RegisterViewModel<T>() where T : TileViewModel
        {
            return (T)Global.Container.Resolve<TileViewModel>(typeof(T).Name);
        }

        private UnitsViewModel RegisterUnitsViewModel()
        {
            return (UnitsViewModel)Global.Container.Resolve<ViewModelBase>(typeof(UnitsViewModel).Name);
        }
    }
}